package com.platform.validator;


import com.platform.comm.util.RegexUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 验证统一社会信用代码
 * @author Tommy
 */
@Target(ElementType.FIELD)
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {UnifiedCode.UnifiedCodeValidate.class})
public @interface UnifiedCode {

    String regexp() default "";

    String message() default "用户名格式不正确";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };


     class UnifiedCodeValidate implements ConstraintValidator<UnifiedCode,String> {

        @Override
        public void initialize(UnifiedCode constraintAnnotation) {

        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            if(StringUtils.isBlank(value)){
                 return true;
            }
            return RegexUtil.isShxym(value);
        }
    }


}
